package zdc.display {

import away3d.bounds.NullBounds;
import away3d.cameras.Camera3D;
import away3d.cameras.lenses.PerspectiveLens;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import away3d.core.math.Plane3D;
import away3d.entities.Mesh;
import away3d.materials.ColorMaterial;
import away3d.materials.TextureMaterial;
import away3d.primitives.CubeGeometry;
import away3d.primitives.PlaneGeometry;

import away3d.primitives.WireframePlane;
import away3d.textures.BitmapTexture;
import away3d.tools.utils.TextureUtils;
import away3d.utils.Cast;

import com.greensock.loading.LoaderMax;

import flash.display.Bitmap;

import flash.display.BitmapData;
import flash.display.DisplayObject;

import flash.display.Scene;

import flash.display.Sprite;
import flash.display3D.textures.Texture;
import flash.display3D.textures.TextureBase;
import flash.events.Event;
import flash.filters.GradientBevelFilter;
import flash.geom.Point;
import flash.geom.Vector3D;

import zdc.Util;
import zdc.util.Away3DUtil;

public class CoverFlowStage3D extends Sprite {

    public static const ITEM_CLICK:String = "CoverFlowStage3D_ITEM_CLICK";

    private var scn:Scene3D;
    private var cam:Camera3D;
    private var view:View3D;
    private var planes:Array;
    private var refPlanes:Array;
    protected var prevImgs:Array;
    protected var viewWid:Number;
    protected var viewHei:Number;
    protected var loader:LoaderMax;
    //正面材质和反射材质
    protected var refMats:Array;
    protected var prvMats:Array;
    protected var startIdx:int = 1;
    protected var itemHei:Number = 350;
    protected var itemWid:Number = 550;
    protected var sx:Number = 0;
    protected var sy:Number = 0;
    protected var margX:Number = 30;
    protected var margY:Number = 30;
    //渐变对象,最佳比例为256 * 256
    protected var gradDio:DisplayObject;
    protected var gradBpd:BitmapData;

    public function CoverFlowStage3D(wid:Number = 0, hei:Number = 0):void {
        viewWid = wid;
        viewHei = hei;

        gradDio = new Grad();
        if (gradDio) {
            gradBpd = new BitmapData(gradDio.width, gradDio.height, true, 0x0);
            gradBpd.draw(gradDio);
        }

        initPreVars();
        initPreStates();
        stage ? init() : addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        initPostVars();
        initStates();
        initEvents();
        run();
    }

    private function initPreVars():void {

    }

    private function initPreStates():void {

    }

    private function initPostVars():void {

    }

    private function initStates():void {
        init3D();
        Util.fullScreen(stage);
    }

    private function initEvents():void {
        addEventListener(Event.ENTER_FRAME, onEnter);
        addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
    }

    private function run():void {
        previewImgs = [
            new Img1(),
            new Img2(),
            new Img3(),
        ];
    }

    //初始化3D对象
    var m:Mesh;

    protected function init3D():void {
        view = new View3D();
        scn = view.scene;
        cam = view.camera;
        view.width = stage.stageWidth;
        view.height = stage.stageHeight;
        view.backgroundColor = 0x00ffffff;
        addChild(view);
    }

    //释放资源
    protected function onRemoved(e:Event):void {
        removeEventListener(Event.ENTER_FRAME, onEnter);
        removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
    }

    //渲染试图
    protected function onEnter(e:Event):void {
        //if(view && planes && planes.length > 0){

        //}

        view && view.render();
    }

    /**
     * 获取预览图片BitmapData数组
     */
    public function get previewImgs():Array {
        return prevImgs;
    }

    /**
     * 设置预览图片BitmapData数组
     * @param value
     */
    public function set previewImgs(value:Array):void {
        if (prevImgs != null) {
            releaseResouce();
        }

        var prvMat:BitmapTexture;
        var refMat:BitmapTexture;
        planes = [];
        refPlanes = [];
        var bmp:Bitmap = new Bitmap();
        var bpd:BitmapData;
        var rbpd:BitmapData;
        var zero:Point = new Point();

        for (var i:int = 0; i < value.length; i++) {
            bpd = Away3DUtil.autoResizeBitmapData(value[i]);
            rbpd = new BitmapData(bpd.width, bpd.height, true, 0);
            gradBpd = Util.sizeBpd(gradBpd, bpd.width, bpd.height);
            rbpd.copyPixels(bpd, bpd.rect, zero, gradBpd, zero, true);
            /*bmp = new Bitmap(rbpd);
             addChild(bmp);
             bmp.x += i * (bmp.width + margX);*/
            prvMat = new BitmapTexture(bpd);
            refMat = new BitmapTexture(rbpd);
            var prv:Mesh = createPlane(i);
            var ref:Mesh = createPlane(i);
            prv.material = new TextureMaterial(prvMat);
            ref.material = new TextureMaterial(refMat);
            prv.pitch(-90);
            ref.pitch(-90);
            ref.rotationX = 90;
            planes.push(prv);
            refPlanes.push(ref);
            scn.addChild(prv);
            scn.addChild(ref);
            //trace(prv.x,  stage.stageWidth);
            ref.x = prv.x = sx + (i - startIdx) * (itemWid + margX);
            prv.y = stage.stageHeight - itemHei >> 1;
            ref.y = prv.y - itemHei;
        }

        prevImgs = value;
    }

    /**
     * 释放资源
     */
    protected function releaseResouce():void {
        removePlanes();
        removeMats();
        clearLoader();
    }

    /**
     * 删除材质
     */
    public function removeMats():void {
        var mesh:Mesh;
        for each(mesh in planes) {
            mesh.material.dispose();
        }
        for each(mesh in refMats) {
            mesh.material.dispose();
        }
    }

    /**
     * 删除平面
     */
    protected function removePlanes():void {
        var plane:Mesh
        if (planes && planes.length > 0) {
            for each(plane in planes) {
                scn.removeChild(plane);
            }
        }
        if (refPlanes && refPlanes.length > 0) {
            for each(plane in refPlanes) {
                scn.removeChild(plane);
            }
        }
        planes = [];
        refPlanes = [];
    }

    /**
     * 创建平面
     * @param idx
     */
    protected function createPlane(idx:int):Mesh {
        return new Mesh(new PlaneGeometry(itemWid, itemHei, 10, 10, true, true));
    }

    /**
     * 清楚loader
     */
    protected function clearLoader():void {
        if (loader) {
            loader.empty();
            loader.unload();
            loader.dispose(true);
        }
    }
}
}